<?php

namespace App\Http\Controllers\Admin\Goods\Api;

use DB;
use App\Model\Goods;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Goods\Goodsrequest;
use App\Http\Controllers\Home\Search\Api\XunSearchApi;
use Illuminate\Support\Facades\Redis; 

class GoodsApi extends Controller
{
	/**
	 * 产品下架
	 */
    public function down(){

		$res = DB::update('update goods set state = 2 where id = ?', [$_GET['id']]);
		if($res){
		    Redis::del('lazyLoad:0');
	        Redis::del('lazyLoad:1');
	        Redis::del('lazyLoad:2');
	        Redis::del('lazyLoad:3');
	        Redis::del('lazyLoad:4');
			Redis::del('goodsData:'.$_GET['id']);//删除之前的产品数据
            Redis::hDel('hotData', 'hotData:'.$_GET['id']);
			echo 1;
		}
	}

	/**
	 * 产品上架
	 */
	public function up(){

		$res = DB::update('update goods set state = 1 where id = ?', [$_GET['id']]);
		if($res){
		    Redis::del('lazyLoad:0');
            Redis::del('lazyLoad:1');
            Redis::del('lazyLoad:2');
            Redis::del('lazyLoad:3');
            Redis::del('lazyLoad:4');
			Redis::del('goodsData:'.$_GET['id']);//删除之前的产品数据
            Redis::hDel('hotData', 'hotData:'.$_GET['id']);
			echo 1;
		}
	}


	/**
	 * 执行某一个商品详情价格的下架
	 * @author wuboheng <[email address]>
	 * @return [void] 
	 */
	public function goodsPricePull(Request $request)
	{
		$id = $request->input('id');
		$gid = $request->input('gid');

		// dd($gid);

		$res = DB::table('goods_price')
			->where('id', $id)
			->update(['state' => 2]);

		if ($res) {

			Redis::del('goodsData:'. $gid);
			Redis::hDel('hotData', 'hotData:'.$gid);

			$data = [
				'code' => 200,
				'msg' => '设置下架成功',
			];
		} else {
			$data = [
				'code' => 404,
				'msg' => '设置下架失败',
			];
		}

		return response()->json($data);

	}


	/**
	 * 执行某一个商品详情价格的上架
	 * @author wuboheng <[email address]>
	 * @return [void] 
	 */
	public function goodsPricePush(Request $request)
	{
		$id = $request->input('id');
		$gid = $request->input('gid');

		$res = DB::table('goods_price')
			->where('id', $id)
			->update(['state' => 1]);

		if ($res) {

			Redis::del('goodsData:'. $gid);
			Redis::hDel('hotData', 'hotData:'.$gid);

			$data = [
				'code' => 200,
				'msg' => '设置上架成功',
			];
		} else {
			$data = [
				'code' => 404,
				'msg' => '设置上架失败',
			];
		}

		return response()->json($data);

	}

	/**
	 * 执行某一个商品详情价格的缺货
	 * @author wuboheng <[email address]>
	 * @return [void] 
	 */
	public function goodsPriceShortage(Request $request)
	{
		$id = $request->input('id');
		$gid = $request->input('gid');

		$res = DB::table('goods_price')
			->where('id', $id)
			->update(['state' => 3]);

		if ($res) {

			Redis::del('goodsData:'. $gid);
			Redis::hDel('hotData', 'hotData:'.$gid);

			$data = [
				'code' => 200,
				'msg' => '设置缺货成功',
			];
		} else {
			$data = [
				'code' => 404,
				'msg' => '设置缺货失败',
			];
		}

		return response()->json($data);

	}



	public function showgood(request $request)
	{
		// dd($request);
		$gname = $request->input('gname');

        $gname = "%".$gname."%";

        $goods = Goods::select(['id', 'goods_sn', 'gname', 'bid',  'sid', 'price', 'pic', 'state'])->where('gname', 'like', $gname)->get();
        
        return view ('Admin/Goods/product-findlist', ['goods' => $goods]);

		 // $xs = new XunSearchApi('goods');

		 // //获取搜索的关键词
   //      if ($request->input('gname') == '') {

   //          $gname = '';

   //      } else {

   //          $gname = $request->input('gname');
   //      }
   //      $search = new XunSearchApi();
   //      $data = $search->doSearch($gname);
   //      $count = $search->countSearchNum();
   //      dd($data);
	}

 	

}
